package com.disease.chronic.Service.Impl;

import com.disease.chronic.Exception.ChronicExceptionEnum;
import com.disease.chronic.Global.Result;
import com.disease.chronic.Mappers.DeviceMapper;
import com.disease.chronic.Service.DeviceService;
import com.disease.chronic.pojo.Device;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    DeviceMapper deviceMapper;
    @Override
    public Result getDevices(String username) {
        List<Device> devices = deviceMapper.selectByUsername(username);
        if(devices.isEmpty()){
            return new Result(ChronicExceptionEnum.NO_DEVICE.getCode(),
                    ChronicExceptionEnum.NO_DEVICE.getMessage());
        }
        return new Result(ChronicExceptionEnum.SUCCESS.getCode(),
                ChronicExceptionEnum.SUCCESS.getMessage(),
                devices);
    }

    @Override
    public String getUsername(String deviceId) {
        return deviceMapper.selectByPrimaryKey(deviceId).getUsername();
    }

    @Override
    public Device getDeviceInfoByDeviceId(String deviceId) {
        return deviceMapper.selectByPrimaryKey(deviceId);

    }

    @Override
    public void setDeviceStatus(String deviceId, String status) {
        Device device = deviceMapper.selectByPrimaryKey(deviceId);
        System.out.println(device.toString());
        System.out.println(status);
        if(status.contains("ON")){
            device.setStatus(1);
        }else{
            device.setStatus(0);
        }
        deviceMapper.updateByPrimaryKey(device);
    }

    @Override
    public Result addDevice(String deviceId ,String username) {
        Device device = deviceMapper.selectByPrimaryKey(deviceId);
        device.setUsername(username);
        deviceMapper.updateByPrimaryKey(device);
        return new Result(ChronicExceptionEnum.SUCCESS.getCode(),
                ChronicExceptionEnum.SUCCESS.getMessage());
    }
}
